<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="销售订单单号" prop="saleOrderNo" label-width="100">
        <el-input
            v-model="queryParams.saleOrderNo"
            placeholder="请输入销售订单单号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户公司" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户公司"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划编号" prop="planId">
        <el-input
          v-model="queryParams.planId"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['production:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['production:plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="销售订单单号" align="center" prop="saleOrderNo" />
      <el-table-column label="客户公司" align="center" prop="customerName" />
      <el-table-column label="订单状态" align="center" prop="saleOrderStatus" >
        <template #default="scope">
          <dict-tag :options="sale_order_status" :value="scope.row.saleOrderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="订单时间" align="center" prop="orderTime" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="计划描述" align="center" prop="planDescribe" />
      <el-table-column label="计划负责人" align="center" prop="userName" />
      <el-table-column label="计划编号" align="center" prop="planNo" />
      <el-table-column label="计划状态" align="center" prop="planStatus"  >
        <template #default="scope">
          <dict-tag :options="plan_status" :value="scope.row.planStatus" />
        </template>
      </el-table-column>
      <el-table-column label="项目实际创建时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目预期开始时间" align="center" prop="planStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目预期结束时间" align="center" prop="planEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目实际结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="138">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['production:plan:edit']">
            {{scope.row.planStatus == 0 ? '编辑':'查看计划详情'}}
          </el-button>
          <el-button link type="primary"  @click="handleUpdatePublic(scope.row)" v-hasPermi="['production:plan:edit']" v-show="scope.row.planStatus == 0">发布计划</el-button>
          <el-button link type="primary"  @click="handleUpdatePublic(scope.row)" v-hasPermi="['production:plan:edit']" v-show="scope.row.planStatus == 3">计划完成</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <PlanAdd v-model="open" v-model:open="open"  :title="title" :id="id" :ins="ins" @getList="getList" />

  </div>
</template>

<script setup name="Plan">
import { listPlan, getPlan, delPlan, addPlan, updatePlan,editPublic } from "@/api/production/plan";
import PlanAdd from "@/views/production/plan/planAdd.vue";
import {defineModel} from "@vue/runtime-core";

const { proxy } = getCurrentInstance();
const { plan_status, sale_order_status } = proxy.useDict("plan_status", "sale_order_status");

const planList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
let id = ref(0);
let ins = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    saleId: null,
    saleOrderNo:null,
    customerName:null,
    saleOrderStatus:null,
    planName: null,
    planDescribe: null,
    status: null,
  },
  rules: {
    saleId: [
      { required: true, message: "销售订单id不能为空", trigger: "blur" }
    ],
    planName: [
      { required: true, message: "计划名称不能为空", trigger: "blur" }
    ],
    planDescribe: [
      { required: true, message: "计划描述不能为空", trigger: "blur" }
    ],
    planId: [
      { required: true, message: "计划编号不能为空", trigger: "blur" }
    ],
    planStartTime: [
      { required: true, message: "项目预期开始时间不能为空", trigger: "blur" }
    ],
    planEndTime: [
      { required: true, message: "项目预期结束时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, rules } = toRefs(data);

/** 查询生产计划列表 */
function getList() {
  loading.value = true;
  listPlan(queryParams.value).then(response => {
    planList.value = response.rows;
    for (let i = 0; i < planList.value.length; i++) {
      planList.value[i].planStatus == null? planList.value[i].planStatus = 0 : planList.value[i].planStatus
    }
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  title.value = "添加生产计划";
}

/** 发布按钮操作 */
function handleUpdatePublic(row) {
  const publicId = {id : row.id , status : null}
  if(row.planStatus == 0){
    publicId.status = 1
  }else if (row.planStatus == 3){
    publicId.status = 4
  }
  editPublic(publicId).then(response => {
    getList();
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  ins.value++;
  if (row.id ===null){
    id.value = 0
    handleAdd();
  }else {
    id.value = row.id
    if (row.planStatus === 0){
      title.value = "修改生产计划";
    }else {
      title.value = "查看计划详情";
    }

    open.value = true;
  }

}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('production/plan/export', {
    ...queryParams.value
  }, `plan_${new Date().getTime()}.xlsx`)
}

getList();
</script>
